//
//  AnimationVC.m
//  Home
//
//  Created by 曹连华 on 2019/6/21.
//  Copyright © 2019 曹连华. All rights reserved.
//

#import "BaseAnimationVC.h"
#import "BaseAnimationViewModel.h"
#import "BaseAnimationTitleTVCellModel.h"
#import "BaseAnimationDelegate.h"
#import <ServiceCode/ServiceCode.h>
#import <Masonry/Masonry.h>
#import <CoreCode/UIColor+Convenience.h>

@interface BaseAnimationVC ()<UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) BaseAnimationViewModel *viewModel;

@property (nonatomic, strong) UIView *animationView;
@property (nonatomic, strong) UIView *animationContentView;
@property (nonatomic, strong) UIButton *groupAnimation;

@end

@implementation BaseAnimationVC

+ (void)load {
    RouterVO *vo = [RouterVO new];
    vo.tab = NO;
    vo.createdType = PBRouteObjectCreateByCode;
    vo.className = NSStringFromClass(self);
    vo.keyInfo = @"基础动画示例页面";
    [[Router sharedInstance]registerRouterVO:vo withKey:@"baseanimation"];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self.viewModel.dataSource;
        _tableView.estimatedRowHeight = 120;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tableView;
}
- (UIView *)animationView {
    if (!_animationView) {
        _animationView = [[UIView alloc]init];
        _animationView.backgroundColor = [UIColor blueColor];
        _animationView.layer.shadowOffset = CGSizeMake(0, -10);
        _animationView.layer.shadowColor = [UIColor redColor].CGColor;
        _animationView.layer.shadowOpacity = 1.f;
        _animationView.layer.shadowRadius = 5.f;
        _animationView.layer.cornerRadius = 15.f;
        
    }
    return _animationView;
}
- (UIView *)animationContentView {
    if(!_animationContentView) {
        _animationContentView = [[UIView alloc]init];
        _animationContentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _animationContentView;
}
- (UIButton *)groupAnimation {
    if (!_groupAnimation) {
        _groupAnimation = [[UIButton alloc]init];
        _groupAnimation.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_groupAnimation setTitle:@"组合动画" forState:UIControlStateNormal];
        [_groupAnimation setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _groupAnimation.titleLabel.font = [UIFont systemFontOfSize:13];
        [_groupAnimation addTarget:self action:@selector(groupAnimationAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _groupAnimation;
}
- (BaseAnimationViewModel *)viewModel {
    if(!_viewModel) {
        _viewModel = [[BaseAnimationViewModel alloc]init];
    }
    return _viewModel;
}
-(void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

- (void)initUI {
    self.navigationItem.title = @"CABasicAnimation";
    
    [self setNaviButtonText:@"恢复原样" textColor:hex(0X2323232) position:BarButtonItemPositionRight];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.animationContentView];
    [self.animationContentView addSubview:self.animationView];
    [self.view addSubview:self.groupAnimation];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.leading.mas_equalTo(0);
        make.width.mas_equalTo(180);
    }];
    
    [self.animationContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(self.tableView.mas_right).mas_equalTo(0);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(self.groupAnimation.mas_top).mas_equalTo(-10);
    }];
    
    [self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    [self.groupAnimation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-30);
        make.bottom.mas_equalTo(-30);
        make.height.mas_equalTo(40);
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self rightBtnClicked:nil];
    
    BaseAnimationTitleTVCellModel *rowModel = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:indexPath];
    CABasicAnimation *animation = [self getAnimationKeyPath:rowModel.subTitle fromValue:rowModel.formValue toValue:rowModel.toValue];
    [self.animationView.layer addAnimation:animation forKey:rowModel.subTitle];
}
- (CABasicAnimation *)getAnimationKeyPath:(NSString *)keyPath fromValue:(id)fromValue toValue:(id)toValue{
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:keyPath];
    basicAnimation.fromValue = fromValue;
    /*byvalue是在fromvalue的值的基础上增加量*/
    //basicAnimation.byValue = @1;
    basicAnimation.toValue = toValue;
    basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    basicAnimation.duration = 2;
    basicAnimation.speed = 2;
    basicAnimation.repeatCount = CGFLOAT_MAX;
    
    //由于CAAnimation的delegate使用的strong类型  声明一个单独的类实现delegate
    basicAnimation.delegate = [BaseAnimationDelegate new];
    basicAnimation.removedOnCompletion = NO;
    
    /*
     * kCAFillModeRemoved : 动画结束后,layer会恢复到之前的状态
     * kCAFillModeForwards : 动画结束后,layer会一直保持着动画最后的状态
     * kCAFillModeBackwards: 这个和kCAFillModeForwards是相对的,就是在动画开始前,你只要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始.你可以这样设定测试代码,将一个动画加入一个layer的时候延迟5秒执行.然后就会发现在动画没有开始的时候,只要动画被加入了layer,layer便处于动画初始状态
     * kCAFillModeBoth: 动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态.
     */
    basicAnimation.fillMode = kCAFillModeForwards;
    return basicAnimation;
}



- (void)groupAnimationAction:(UIButton *)btn {
    [self.animationView.layer removeAllAnimations];
    
    BaseAnimationTitleTVCellModel *rowModel0 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:0 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel4 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:4 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel8 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:8 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel14 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:14 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel15 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:15 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel16 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:16 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel17 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:17 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel18 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:18 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel19 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:19 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel20 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:20 inSection:0]];
    BaseAnimationTitleTVCellModel *rowModel21 = (BaseAnimationTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:[NSIndexPath indexPathForRow:21 inSection:0]];
    CABasicAnimation *animation0 = [self getAnimationKeyPath:rowModel0.subTitle fromValue:rowModel0.formValue toValue:rowModel0.toValue];
    CABasicAnimation *animation4 = [self getAnimationKeyPath:rowModel4.subTitle fromValue:rowModel4.formValue toValue:rowModel4.toValue];
    CABasicAnimation *animation8 = [self getAnimationKeyPath:rowModel8.subTitle fromValue:rowModel8.formValue toValue:rowModel8.toValue];
    CABasicAnimation *animation14 = [self getAnimationKeyPath:rowModel14.subTitle fromValue:rowModel14.formValue toValue:rowModel14.toValue];
    CABasicAnimation *animation15 = [self getAnimationKeyPath:rowModel15.subTitle fromValue:rowModel15.formValue toValue:rowModel15.toValue];
    CABasicAnimation *animation16 = [self getAnimationKeyPath:rowModel16.subTitle fromValue:rowModel16.formValue toValue:rowModel16.toValue];
    CABasicAnimation *animation17 = [self getAnimationKeyPath:rowModel17.subTitle fromValue:rowModel17.formValue toValue:rowModel17.toValue];
    CABasicAnimation *animation18 = [self getAnimationKeyPath:rowModel18.subTitle fromValue:rowModel18.formValue toValue:rowModel18.toValue];
    CABasicAnimation *animation19 = [self getAnimationKeyPath:rowModel19.subTitle fromValue:rowModel19.formValue toValue:rowModel19.toValue];
    CABasicAnimation *animation20 = [self getAnimationKeyPath:rowModel20.subTitle fromValue:rowModel20.formValue toValue:rowModel20.toValue];
    CABasicAnimation *animation21 = [self getAnimationKeyPath:rowModel21.subTitle fromValue:rowModel21.formValue toValue:rowModel21.toValue];
    
    CAAnimationGroup *animaGroup = [CAAnimationGroup animation];
    animaGroup.duration = 5.0f;
    animaGroup.fillMode = kCAFillModeForwards;
    animaGroup.removedOnCompletion = NO;
    animaGroup.animations = @[animation0,animation4,animation8,animation14,animation15,animation16,animation17,animation18,animation19,animation20,animation21];
    [self.animationView.layer addAnimation:animaGroup forKey:@"AnimationGroup"];
    
}

- (void)rightBtnClicked:(id)sender {
    [self.animationView.layer removeAllAnimations];
}


/*
 CATransform3D Key Paths
 旋转x,y,z分别是绕x,y,z轴旋转
 static NSString *kCARotation = @"transform.rotation";
 static NSString *kCARotationX = @"transform.rotation.x";
 static NSString *kCARotationY = @"transform.rotation.y";
 static NSString *kCARotationZ = @"transform.rotation.z";

 缩放x,y,z分别是对x,y,z方向进行缩放
 static NSString *kCAScale = @"transform.scale";
 static NSString *kCAScaleX = @"transform.scale.x";
 static NSString *kCAScaleY = @"transform.scale.y";
 static NSString *kCAScaleZ = @"transform.scale.z";

 平移x,y,z同上
 static NSString *kCATranslation = @"transform.translation";
 static NSString *kCATranslationX = @"transform.translation.x";
 static NSString *kCATranslationY = @"transform.translation.y";
 static NSString *kCATranslationZ = @"transform.translation.z";

 平面
 CGPoint中心点改变位置，针对平面
 static NSString *kCAPosition = @"position";
 static NSString *kCAPositionX = @"position.x";
 static NSString *kCAPositionY = @"position.y";

 CGRect
 static NSString *kCABoundsSize = @"bounds.size";
 static NSString *kCABoundsSizeW = @"bounds.size.width";
 static NSString *kCABoundsSizeH = @"bounds.size.height";
 static NSString *kCABoundsOriginX = @"bounds.origin.x";
 static NSString *kCABoundsOriginY = @"bounds.origin.y";

 透明度
 static NSString *kCAOpacity = @"opacity";
 背景色
 static NSString *kCABackgroundColor = @"backgroundColor";
 圆角
 static NSString *kCACornerRadius = @"cornerRadius";
 边框
 static NSString *kCABorderWidth = @"borderWidth";
 阴影颜色
 static NSString *kCAShadowColor = @"shadowColor";
 偏移量CGSize
 static NSString *kCAShadowOffset = @"shadowOffset";
 阴影透明度
 static NSString *kCAShadowOpacity = @"shadowOpacity";
 阴影圆角
 static NSString *kCAShadowRadius = @"shadowRadius";
 */
@end
